﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Cms.Web.Context;
using Umbraco.Cms.Web.Model.BackOffice;
using Umbraco.Cms.Web.Model.BackOffice.PropertyEditors;
using Umbraco.Framework;

namespace Umbraco.Cms.Web.PropertyEditors.TreeMultiNodePicker
{
	[PropertyEditor("A2906758-4A44-402A-B5EC-4736953E2DFA", "TreeMultiNodePicker", "Tree Multi-Node Picker")]
	public class TreeMultiNodePickerEditor : PropertyEditor<TreeMultiNodePickerEditorModel>
	{
		private IUmbracoApplicationContext _appContext;

		public TreeMultiNodePickerEditor(IUmbracoApplicationContext appContext)
		{
			_appContext = appContext;
		}

		public override TreeMultiNodePickerEditorModel CreateEditorModel()
		{
			var model = new TreeMultiNodePickerEditorModel();
			var url = CreateUrlHelper();

			var treeName = "ContentTree";


			var treeId = new Guid(CorePluginConstants.ContentTreeControllerId);


			var treeUrl = url.GetTreeUrl(treeName, new HiveId(treeId), treeId,
								new
								{
									//specify that the tree is in dialog mode
									DialogMode = true,
									//specify the onClick JS method handler for the node
									OnNodeClick = "tmnpClick"
								});


			// var treeUrl = "/TestVDir/Umbraco/Trees/ContentTree/Index/guid%24_bbc025080f80491f9a2a193354b1b6b3?DialogMode=True&OnNodeClick=Umbraco.Editors.MoveCopyDialog.nodeClickHandler";
			//                /TestVDir/Umbraco/Content/Index/guid%24_bbc025080f80491f9a2a193354b1b6b3?treeId=bbc025080f80491f9a2a193354b1b6b3?DialogMode=True

			//model.Value = new List<string>() { "hello", "world" };

			model.TreeRenderModel = new TreeRenderModel(treeUrl, "treeMultiNodePicker")
					{
						ShowContextMenu = false
					};

			return model;
		}

		private UrlHelper CreateUrlHelper()
		{
			var httpContext = HttpContext.Current;
			var httpContextBase = new HttpContextWrapper(httpContext);
			var routeData = new RouteData();
			var requestContext = new RequestContext(httpContextBase, routeData);

			return new UrlHelper(requestContext);
		}
	}
}
